package com.havey.ye.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.havey.ye.dto.ChatRecordsQueryDTO;
import com.havey.ye.dto.CommonPageResponseDTO;
import com.havey.ye.entity.ChatRecords;
import com.havey.ye.service.IChatRecordsService;

import jakarta.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yehuaiwei
 * @since 2025-01-22
 */
@RestController
@RequestMapping("/chat-records")
public class ChatRecordsController {

    @Resource
    private IChatRecordsService iChatRecordsService;

    /**
     * 查询聊天记录列表。
     * 1. 入参支持分页查询
     * 2. 还要支持根据creted_by查询。
     * 3. 支持created_at 的时间排序查询
     * 4. 支持问题内容的answer_content的模拟匹配
     * 5. 支持question_content的模糊匹配
     * 6. 支持session_id的精准匹配查询
     * 使用mybatis-plus的分页插件实现
     */
    @PostMapping("/list")
    public CommonPageResponseDTO<List<ChatRecords>> getChatRecords(@RequestBody ChatRecordsQueryDTO queryDTO) {
        // 把queryDTO变成MyabtisPlus的查询条件Wrapper
        LambdaQueryWrapper<ChatRecords> wrapper = new LambdaQueryWrapper<>();
        // MyabtisPlus的分页查询
        Page<ChatRecords> page = new Page<>(queryDTO.getPage(), queryDTO.getSize());
        if (queryDTO.getStartCreatedAt() != null) {
            wrapper.ge(ChatRecords::getCreatedAt, queryDTO.getStartCreatedAt());
        }
        if (queryDTO.getEndCreatedAt() != null) {
            wrapper.lt(ChatRecords::getCreatedAt, queryDTO.getEndCreatedAt());
        }
        if (StringUtils.isNotBlank(queryDTO.getSessionId())) {
            wrapper.eq(ChatRecords::getSessionId, queryDTO.getSessionId());
        }
        if (StringUtils.isNotBlank(queryDTO.getQuestionContent())) {
            wrapper.like(ChatRecords::getQuestionContent, queryDTO.getQuestionContent());
        }
        if (StringUtils.isNotBlank(queryDTO.getAnswerContent())) {
            wrapper.like(ChatRecords::getAnswerContent, queryDTO.getAnswerContent());
        }
        if (StringUtils.isNotBlank(queryDTO.getCreatedBy())) {
            wrapper.eq(ChatRecords::getCreatedBy, queryDTO.getCreatedBy());
        }
        wrapper.orderByDesc(ChatRecords::getCreatedAt);
        // 初始化iChatRecordsService.page及其返回对象

        CommonPageResponseDTO<List<ChatRecords>> responseDTO = new CommonPageResponseDTO<>();
        Page<ChatRecords> rowPage = iChatRecordsService.page(page, wrapper);
        responseDTO.setData(rowPage.getRecords());
        responseDTO.setCode(200);
        responseDTO.setMessage("Success");
        responseDTO.setTotal(rowPage.getTotal());
        responseDTO.setPage(rowPage.getCurrent() - 1);
        responseDTO.setSize(rowPage.getSize());
        responseDTO.setTotalPage(responseDTO.getTotal() / rowPage.getSize());
        return responseDTO;
    }
}
